<!-- 轮播图组件 -->
<template>
  <div>
    <div class="swiper-container">
      <div class="swiper-wrapper">
        <div class="swiper-slide" v-for="item in list" :key="item.id">
          <img :src="item.address" alt="" v-if="item.address"/>
          <img :src="item" alt="" v-else>
        </div>
      </div>
      <!-- 如果需要分页器 -->
      <div class="swiper-pagination"></div>

      <!-- 如果需要滚动条 -->
      <!-- <div class="swiper-scrollbar"></div> -->
    </div>
  </div>
</template>

<script>
import Swiper from "swiper";
import "swiper/css/swiper.min.css";
import axios from "axios";
export default {
  data() {
    return {
      list: [],
    };
  },
  async created() {//要异步获取数据，不然要报错
    let re="";
    if(this.$route.path=="/"){
      re = await axios.get("http://localhost:3000/banner"); 
      this.list = re.data;
    }
    else{
      re = await axios.get("http://localhost:3000/details");
      this.list = re.data.imgs;
    }
  },
  updated() {//轮播图请求要放在这个生命周期中，不然加载卡顿，有错误，滚动异常
    new Swiper(".swiper-container", {
      // direction: 'vertical', // 垂直切换选项
      loop: true, // 循环模式选项
      autoplay: {
        delay: 1500,
        disableOnInteraction:false
      },
      // 如果需要分页器
      pagination: {
        el: ".swiper-pagination",
      },
      // 如果需要滚动条
      scrollbar: {
        el: ".swiper-scrollbar",
      },
    });
  },
};
</script>

<style lang="less" scoped>
.swiper-container {
  width: 95vw;
  height: 30vh;
  margin: 0.5vw auto;
  img {
    width: 100%;
    height: 100%;
  }
}
</style>